package service

import (
	"chat/model"
	"chat/serializer"
	"fmt"
)

type UserRegisterService struct {
	UserName string `json:"user_name" "form:user_name"`
	Password string `json:"password" "form:password"`
}

// 用户注册
func (service *UserRegisterService) Register() serializer.Response {

	var user model.User
	// user用来接受
	code := 200
	var count int64 = 0
	fmt.Println("传入的service", service)
	model.DB.Model(&model.User{}).Where("user_name=?", service.UserName).First(&user).Count(&count)
	if count != 0 {
		// 说明用户名已经存在
		return serializer.Response{
			Status: 400,
			Msg:    "用户已经存在",
		}
	}
	user = model.User{
		UserName: service.UserName,
	}

	// 密码进行加密
	if err := user.SetPassword(service.Password); err != nil {
		return serializer.Response{
			Status: 500,
			Msg:    "密码加密出错",
		}
	}
	fmt.Println("注册的user", user)
	// 插入数据
	model.DB.Create(&user)
	return serializer.Response{
		Status: code,
		Msg:    "注册成功",
		Data:   user,
	}
}
